import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/components/empty_widget.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/model/exam_rank.dart';
import 'package:kxy_flutter_app/view/my/rank/user_rank_item_widget.dart';
import 'package:provider/provider.dart';

import 'view_model/exam_rank_model.dart';

class ExamRankListWidget extends StatelessWidget {
  final int cateIndex;

  ExamRankListWidget({
    Key key,
    @required this.cateIndex,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<ExamRankModel>(
      builder: (cxt, vm, child) {
        return FutureBuilder<ExamRank>(
          future: vm.getRank(cateIndex),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return examListWidget(snapshot.data, context);
            }
            return LoadingWidget();
          },
        );
      },
    );
  }

  Widget examListWidget(ExamRank rank, BuildContext context) {
    if (rank == null || rank.frontRankList.length == 0) {
      return Container(
        padding: EdgeInsets.only(top: 40),
        child: EmptyWidget(),
      );
    }

    return Container(
      color: Colors.white,
      child: ListView(
        children: ListTile.divideTiles(
          tiles: rank.frontRankList.map((e) {
            return UserRankItemWidget(rankItem: e);
          }).toList(),
          context: context,
          color: Colors.black12,
        ).toList(),
      ),
    );
  }
}
